package com.welove.http;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class SubmitDataByHttpClient {

	
	/**
	 * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a>  Dylan
	 * 本类封装了Android中向web服务器提交数据的两种方式四种方法
	 */
	

		/**
		 * 使用get请求以普通方式提交数据
		 * @param map 传递进来的数据，以map的形式进行了封装
		 * @param path 要求服务器servlet的地址
		 * <a href="http://my.oschina.net/u/556800" class="referer" target="_blank">@return</a>  返回的boolean类型的参数
		 * @throws Exception
		 */
		public boolean submitDataByDoGet(Map<String, String> map, String path) throws Exception {
			// 拼凑出请求地址
			
			List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); 
//			StringBuilder sb = new StringBuilder(path);
//			sb.append("?");
			for (Map.Entry<String, String> entry : map.entrySet()) {
//				sb.append(entry.getKey()).append("=").append(entry.getValue());
//				sb.append("&");
				
				params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			
//			
//			 
//			params.add(new BasicNameValuePair("param2", "value2")); 
			//对参数编码 
			String param = URLEncodedUtils.format(params, "UTF-8"); 
			//baseUrl 
			
			//将URL与参数拼接 
			HttpGet getMethod = new HttpGet(path + "?" + param); 

			HttpClient httpClient = new DefaultHttpClient(); 
			try { 
			HttpResponse response = httpClient.execute(getMethod); //发起GET请求 
			
	
			} 
			 catch (Exception e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
			} 
			
//
			return false;
		}

		

		/**
		 * 以HttpClient的DoGet方式向服务器发送请数据
		 * @param map 传递进来的数据，以map的形式进行了封装
		 * @param path 要求服务器servlet的地址
		 * <a href="http://my.oschina.net/u/556800" class="referer" target="_blank">@return</a>  返回的boolean类型的参数
		 * @throws Exception
		 */
		public String submitDataByHttpClientDoGet(Map<String, String> map, String path) throws Exception {
			
			String result="";
			List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); 
//			StringBuilder sb = new StringBuilder(path);
//			sb.append("?");
			for (Map.Entry<String, String> entry : map.entrySet()) {
//				sb.append(entry.getKey()).append("=").append(entry.getValue());
//				sb.append("&");
				
				params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			
//			params.add(new BasicNameValuePair("param2", "value2")); 
			//对参数编码 
			String param = URLEncodedUtils.format(params, "UTF-8"); 
			
			
			//baseUrl 
			
			//将URL与参数拼接 
			HttpGet getMethod = new HttpGet(path + "?" + param); 
			
			HttpClient httpClient = new DefaultHttpClient(); 
			try { 
			HttpResponse response = httpClient.execute(getMethod); //发起GET请求 
			if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				
				
				HttpEntity entity = response.getEntity();
				if(null !=entity)
				{
					entity = new BufferedHttpEntity(entity);
					
					result=EntityUtils.toString(entity);
				}
				return result;
			}
			
	
			} 
			 catch (Exception e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
			} 
			return result;
		}
		
		

	
}
